﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses.Data_Adapters;

namespace CECB
{
    /// <summary>
    /// Interaction logic for Project_Questions_Window_Update.xaml
    /// </summary>
    public partial class Project_Questions_Window_Update : Window
    {
        public Project_Questions_Window_Update()
        {
            InitializeComponent();
            cecbContext = new DSSEntities();
            ProjectAutocompleteTextBox.ItemsSource = cecbContext.Projects;

            cecbClasses.Data_Adapters.Func_Impacts impacts = new Func_Impacts();
            comboBoxComponents.ItemsSource = impacts.getComponentMainCat(ProjectAutocompleteTextBox.Text);
            listBoxActivities.ItemsSource = impacts.getImpacts(ProjectAutocompleteTextBox.Text, comboBoxComponents.Text);
        }
        string impact;
        string impactReference;
        DSSEntities cecbContext;

        private void ProjectAutocomplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Projects proj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
            ProjectDetailsWrapPanel.DataContext = proj;
        }

        private void comboBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ProjectAutocompleteTextBox.Text == "")
            {
                MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else
            {

                cecbClasses.Data_Adapters.Func_Impacts impacts = new Func_Impacts();
                listBoxActivities.ItemsSource = impacts.getImpacts(ProjectAutocompleteTextBox.Text, comboBoxComponents.Text);
                listBoxActivities.Visibility = Visibility.Visible;

            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            cecbClasses.Data_Adapters.Func_Impacts impacts = new Func_Impacts();

            if (textBoxImpactUpdate.Text == "")
            {
                MessageBox.Show("Nothing has been modified", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else
            {
                if (impacts.updateImpactsDetails(impactReference.ToString(), textBoxImpactUpdate.Text.ToString()))
                {
                    MessageBox.Show("Project Impact Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    textBoxImpactUpdate.Clear();
                    int x = comboBoxComponents.SelectedIndex;
                    comboBoxComponents.SelectedIndex = -1;
                    comboBoxComponents.SelectedIndex = x;
                }
                else
                {
                    MessageBox.Show("Project Impact Updation failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                listBoxActivities.ItemsSource = impacts.getImpacts(ProjectAutocompleteTextBox.Text, comboBoxComponents.Text);

            }
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {



            MessageBox.Show("Project Impact Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);



            comboBoxComponents.SelectedIndex = -1;
        }

        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxActivities.Items.Count > 0)
            {
                try
                {
                    Impacts getImpact = (Impacts)listBoxActivities.SelectedItem;
                    textBoxImpactUpdate.Text = getImpact.impt_name.ToString();
                    impact = getImpact.impt_name.ToString();
                    textBoxImpactUpdate.Text = impact.ToString();
                    impactReference = getImpact.impt_reference;

                }
                catch (Exception)
                {


                }
            }
        }
    }
}